//
//  LRXDistributionTableView.m
//  路人侠
//
//  Created by zhangxulong on 4/4/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXDistributionTableView.h"
#import "LRXDistributionViewCell.h"
#import "LRXDistributionFirstCell.h"
@interface LRXDistributionTableView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,weak) LRXHelpMeInfoModel * itemInfo;
@property (nonatomic,weak) UIView * headContentView;
@property (nonatomic,weak) UIButton * peisongBtn;
@end
@implementation LRXDistributionTableView

- (void)layoutDistributionSubView:(LRXHelpMeInfoModel *)itemInfo
{
    
    self.itemInfo = itemInfo;
    UIView * headView = [[UIView alloc]init];
    headView.frame = CGRectMake(0, 0, self.frame.size.width, 441 * hScale);
    headView.backgroundColor = mainColor;
    self.headContentView = headView;
    
    UIView * headContentView = [[UIView alloc]init];
    headContentView.backgroundColor = [UIColor whiteColor];
    headContentView.layer.cornerRadius = 10 * hScale;
    headContentView.frame = CGRectMake(34 * wScale, 48 * hScale, 572 * wScale, 332 * hScale);
    
    //设置contentView 的内容
    //设置时间
    CGFloat timeY = 49 * hScale;
    
    UILabel * stTimeLabel = [[UILabel alloc]init];
    stTimeLabel.textAlignment = NSTextAlignmentCenter;
    stTimeLabel.text = itemInfo.startTime;
    stTimeLabel.textColor = mainColor;
    stTimeLabel.font = [UIFont systemFontOfSize:34 * hScale];
    CGFloat stTimeX = 121 * wScale;
    stTimeLabel.frame = CGRectMake(stTimeX,timeY , 0, 0);
    [stTimeLabel sizeToFit];
    
  
    
    UIView * lineView = [[UIView alloc]init];
    lineView.backgroundColor = mainColor;
    lineView.frame = CGRectMake(CGRectGetMaxX(stTimeLabel.frame) + 10 * wScale, stTimeLabel.center.y - 2 * hScale, 25 * wScale, 2 * hScale);
    
    
    UILabel * endTimeLabel = [[UILabel alloc]init];
    endTimeLabel.textAlignment = NSTextAlignmentCenter;
    endTimeLabel.font = stTimeLabel.font;
    endTimeLabel.text = itemInfo.endTime;
    endTimeLabel.textColor = stTimeLabel.textColor;
    CGFloat endTimeX = CGRectGetMaxX(lineView.frame) + 10 * wScale;
    endTimeLabel.frame = CGRectMake(endTimeX, timeY, 0, 0);
    [endTimeLabel sizeToFit];
    
    [headContentView addSubview:lineView];
    [headContentView addSubview:stTimeLabel];
    [headContentView addSubview:endTimeLabel];
    
    UIButton * phoneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [phoneBtn setImage:[UIImage imageNamed:@"style_phone_icon"] forState:UIControlStateNormal];
    phoneBtn.frame = CGRectMake(428 * wScale, 29 * hScale, 75 * hScale, 75 * hScale);
    [phoneBtn addTarget:self action:@selector(callPhoneNum:) forControlEvents:UIControlEventTouchUpInside];
    [headContentView addSubview:phoneBtn];
    
    //地点设置
    CGFloat addrFont = 28 * hScale;
    CGFloat spotRadius = 16 * hScale;
    CGFloat stLabelW = 400 * wScale;
    CGFloat stlabelY = 124 * hScale;
    //开始地点和结束地点

    UILabel * endLabel = [[UILabel alloc]init];

    endLabel.text = itemInfo.sendAddr;
    endLabel.textColor = [UIColor blackColor];
    
    
    endLabel.font = [UIFont systemFontOfSize:addrFont];
    CGSize fontSize = [endLabel.text boundingRectWithSize:CGSizeMake(stLabelW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:addrFont]} context:nil].size;
    endLabel.frame = CGRectMake(100 * wScale, stlabelY , fontSize.width, fontSize.height);  //+ 42 * hScale
    endLabel.numberOfLines = 0;

   
    
    zxlCircleView *  endSpot = [[zxlCircleView alloc]init];
    endSpot.imageName = @"endSpot";
    endSpot.borderColor = [UIColor clearColor];
    endSpot.lineWidth = 0;
    
    //结束点
    endSpot.frame = CGRectMake(56 * wScale, endLabel.frame.origin.y + 10 * hScale, spotRadius, spotRadius);
    
    endSpot.backgroundColor = [UIColor clearColor];
    

    [headContentView addSubview:endLabel];

    [headContentView addSubview:endSpot];
    
    NSString * nowStrH = [NSString stringWithFormat:@"%02ld",[LRXGetTime getHour]];
    
    UIButton * accpSendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [accpSendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [accpSendBtn setTitleColor:GraylineColor forState:UIControlStateDisabled];
    accpSendBtn.backgroundColor = mainColor;
    [accpSendBtn setTitle:@"开始配送" forState:UIControlStateNormal];
    accpSendBtn.titleLabel.font = [UIFont systemFontOfSize:32 * hScale];
    accpSendBtn.frame = CGRectMake(314 * wScale, 255 * hScale, 208 * wScale, 59 * hScale);
    accpSendBtn.layer.cornerRadius = 5 * hScale;
    accpSendBtn.layer.masksToBounds = YES;
    self.peisongBtn = accpSendBtn;
    
    NSRange range = {0,2};
    NSInteger startH = [[itemInfo.startTime substringWithRange:range] integerValue];
    NSInteger nowH = [nowStrH integerValue];
    if ((startH - nowH) <= 1) {
        accpSendBtn.enabled = YES;
    }
    else
        accpSendBtn.enabled = NO;
    
    
    if (itemInfo.orderModel.status == 2) {
        [accpSendBtn setTitle:@"正在配送" forState:  UIControlStateNormal];
        accpSendBtn.enabled = NO;
    }
    else if(itemInfo.orderModel.status == 3)
    {
        [accpSendBtn setTitle:@"已完成配送" forState:  UIControlStateNormal];
        [self accpetSend];
    }
    
    
    
    [accpSendBtn addTarget:self action:@selector(accpetOrderSend) forControlEvents:UIControlEventTouchUpInside];
    
    [headContentView addSubview:accpSendBtn];
    
    [headView addSubview:headContentView];
    [self addSubview:headView];
    
    CGFloat cellHeight = 90 * hScale;
    
    
    
    LRXDistributionFirstCell * firstCell = [[LRXDistributionFirstCell alloc]init];
    firstCell.frame = CGRectMake(0, CGRectGetMaxY(headView.frame), self.frame.size.width, cellHeight);
    NSDictionary * cellInfo = @{@"profile_url":self.itemInfo.userInfo.userImage,@"chat":@"填写什么呢？？"};
    [firstCell layoutDistributionFirstCell:cellInfo];
    

    UITableView * tableView = [[UITableView alloc]init];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = cellHeight;
    tableView.frame = CGRectMake(0, CGRectGetMaxY(headView.frame) + tableView.rowHeight, self.frame.size.width, self.frame.size.height - CGRectGetMaxY(firstCell.frame));
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    [self addSubview:firstCell];
    [self addSubview:tableView];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.itemInfo.thingArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * DISTRIBUTIONVIEWCELLED = @"DISTRIBUTIONVIEWCELLED";
    LRXDistributionViewCell * cell = [tableView dequeueReusableCellWithIdentifier:DISTRIBUTIONVIEWCELLED];
    if (cell == nil) {
        cell = [[LRXDistributionViewCell alloc]init];
        [cell layoutDistributionCell:self.itemInfo.thingArray[indexPath.row]];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}
/**
 *  打电话
 *
 *  @param phoneNumBtn 求带者的电话
 */
- (void)callPhoneNum:(UIButton * )phoneNumBtn
{
    NSString * phoneNum = self.itemInfo.phoneNum;
    NSString * phoneUrl = [NSString stringWithFormat:@"tel://%@",phoneNum];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneUrl]];
}
- (void)accpetOrderSend
{
    
    AFHTTPSessionManager * manger = [AFHTTPSessionManager manager];
    [MBProgressHUD showMessage:nil toView:self];
    
    NSDictionary * parme = @{@"w":[LRXBlowFish enCodeBlowfishWithValue:self.itemInfo.itemID withKeyStr:blowFishKey]};
    [manger POST:startDeliveryUrl parameters:parme progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:self animated:YES];
            NSInteger statuCode = [responseObject[@"data"][@"status_code"] integerValue];
            if (statuCode == 1) {
                [self accpetSend];
            }
            else
            {
                NSLog(@"%@",responseObject);
            }
        });

      
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
        
        [MBProgressHUD hideHUDForView:self animated:YES];
    }];
}

- (void)accpetSend
{
    UIView * accpBack = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headContentView.frame), MainScreenW, self.frame.size.height - CGRectGetMaxY(self.headContentView.frame))];
    accpBack.backgroundColor = [UIColor blackColor];
    accpBack.alpha = 0.2;
    
    
    [self.peisongBtn setTitle:@"正在配送" forState:UIControlStateNormal];
    
//    UIImageView * p = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"accpetOrder"]];
//    p.frame = CGRectMake(319 * wScale, CGRectGetMaxY(self.headContentView.frame) + 269 * hScale, 286 * wScale, 222 * hScale);
//    [self addSubview:p];
    [self addSubview:accpBack];
}
@end
